<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>我的购物车</title>
<%@include file="inc/common_head.jsp"%>
</head>
<script type="text/javascript">
	jQuery(function($) {
		/* 购买数量输入框绑定改变事件 */
		$("input[cartid]").change(function() {
				// 将当前的DOM对象，保存到变量中
				var buynum = $(this).val();
				var that = this;
				if (buynum.match("^\\d+$") && buynum > 0) {
					$.ajax({
								url : "${path}/cart",
								type : "post",
								data : {
									method : "changnum",
									gid : $(this).attr("cartid"),
									buynum : buynum
								},
								/* 	dataType:"text", */
								success : function(result) {
									//alert(result);
									if (result == "tologin") {
										location = "${path}/login.jsp";
										return;
									}
									if (result == "ok") {
										//更改小计
										var buynum = $(that).val();
										var gid = $(that).attr("cartid");
										var estoreprice = $(that).attr("estoreprice");
										// 计算小计金额，并保留2位小数
										var xj = (estoreprice * buynum).toFixed(2);
										$("td[xjid=" + gid + "]").text(xj + "元");

										//更改总金额
										var total = 0;
										$("td[xjid]").each(function() {
															total = total+ parseFloat($(this).text());
														});
										$("#total").text(total.toFixed(2));
										//更改节省金额
										var json = 0;
										$("tr#fang").each(function() {
															// 从当列中获取find[xxx]的行，获取属性
															var buynum = $(this).find("input[cartid]").val();
															var eprice = parseFloat($(this).find("td[est]").text());
															var mprice = parseFloat($(this).find("td[mar]").text());
															json = (mprice - eprice)* buynum+ json;
														});

										$("#json").text(json.toFixed(2));
									}
								}
							});

				} else {
					alert("输入不正确");
					location = "${path}/cart?method=query";
					return;
				}

		});

		/* 删除绑定点击事件 */
		$("a[did]").on("click",function() {
					if (confirm("是否确认删除？")) {
						//location = $(this).attr("url");
						var that = this;
						$.ajax({
									url : "${path}/cart",
									type : "post",
									data : {
										method : "delcart",
										gid : $(this).attr("did")
									},
									dataType : "text",
									success : function(result) {
										if (result == "tologin") {
											location = "${path}/login.jsp";
											return;
										}

										if (result == "ok") {
											//删除本身所在的那一行,函数内this不可用，调用变量that
											$(that).closest("tr").remove();

											//购物车没有货物，删除所在表格
											var num = $("#buycar tr").size();
											if (num == 2) {
												$("#buycar").replaceWith("<a href='${path}/goods?method=query'>空空如也，去逛逛</a");
											}
											//更改小计
											var buynum = $(that).val();
											var gid = $(that).attr("cartid");
											var estoreprice = $(that).attr("estoreprice");
											// 计算小计金额，并保留2位小数
											var xj = (estoreprice * buynum).toFixed(2);
											$("td[xjid=" + gid + "]").text(xj + "元");

											//更改总金额
											var total = 0;
											$("td[xjid]").each(function() {
																total = total+ parseFloat($(this).text());
															});
											$("#total").text(
													total.toFixed(2));

											//更改节省金额
											var json = 0;
											$("tr#fang").each(function() {
																// 从当列中获取find[xxx]的行，获取属性
																var buynum = $(this).find("input[cartid]").val();
																var eprice = parseFloat($(this).find("td[est]").text());
																var mprice = parseFloat($(this).find("td[mar]").text());
																json = (mprice - eprice)* buynum+ json;
															});
											$("#json").text(json.toFixed(2));
										}
									}
								});
					}
			});

	});
</script>
<body>
	<%@include file="inc/header.jsp"%>
	<div class="block table">
		<div class="AreaR">
			<div class="block box">
				<div class="blank"></div>
				<div id="ur_here">
					当前位置: <a href="index.jsp">首页</a>
					<code>&gt;</code>
					我的购物车
				</div>
			</div>
			<div class="blank"></div>
			<div class="box">
				<div class="box_1">
					<div class="userCenterBox boxCenterList clearfix"
						style="_height: 1%;">
						<h5>
							<span>我的购物车</span>
						</h5>
						<table width="100%" align="center" border="0" cellpadding="5"
							cellspacing="1" bgcolor="#dddddd" id="buycar">
							<tr>
								<th bgcolor="#ffffff">商品名称</th>
								<th bgcolor="#ffffff">市场价</th>
								<th bgcolor="#ffffff">本店价</th>
								<th bgcolor="#ffffff">购买数量</th>
								<th bgcolor="#ffffff">小计</th>
								<th bgcolor="#ffffff" width="160px">操作</th>
							</tr>
							<c:set var="total" value="0"></c:set>
							<c:set var="aaa" value="0"></c:set>
							<c:forEach items="${clist }" var="c">
								<tr id="fang">
									<td bgcolor="#ffffff" align="center" style="width: 300px;">
										<!-- 商品图片 --> <a href="javascript:;" target="_blank"> <img
											style="width: 80px; height: 80px;" src="${c.goods.imgurl }"
											border="0" title="${c.goods.name}" />
									</a><br /> <!-- 商品名称 --> <a href="javascript:;" target="_blank"
										class="f6">${c.goods.name}</a>
									</td>
									<td mar align="center" bgcolor="#ffffff"><fmt:formatNumber
											value="${c.goods.marketprice}" pattern="#.00" />元</td>
									<td est align="center" bgcolor="#ffffff"><fmt:formatNumber
											value="${c.goods.estoreprice}" pattern="#.00" />元</td>
									<td align="center" bgcolor="#ffffff"><input
										value="${c.buynum }" class="cartInput" cartid="${c.gid}"
										estoreprice="${c.goods.estoreprice}" /></td>
									<td align="center" bgcolor="#ffffff" xjid="${c.gid}"><fmt:formatNumber
											value="${c.goods.estoreprice*c.buynum}" pattern="#.00" />元</td>
									<c:set var="total"
										value="${c.goods.estoreprice*c.buynum+total}"></c:set>
									<c:set var="aaa"
										value="${(c.goods.marketprice-c.goods.estoreprice)*c.buynum+aaa}"></c:set>
									<td align="center" bgcolor="#ffffff"><a
										href="javascript:;" class="f6" did="${c.gid}">删除</a></td>
								</tr>
							</c:forEach>


							<tr>
								<td colspan="6"
									style="text-align: right; padding-right: 10px; font-size: 25px;">
									购物金额小计&nbsp;<font color="red" id="total"> <fmt:formatNumber
											value="${total}" pattern="#.00" /></font>元， 共为您节省了&nbsp; <font
									id="json" color="red"><fmt:formatNumber value="${aaa}"
											pattern="#.00" /></font>元 <a
									href="${path}/cart?method=query&to=orders"><input
										value="去结算" type="button" class="btn" /></a>
								</td>
							</tr>
						</table>
					</div>
				</div>
			</div>
		</div>
		<div class="blank"></div>
		<div class="blank5"></div>
	</div>
	<%@include file="inc/footer.jsp"%>
</body>
</html>
